﻿using CraftParameterConfig.Domain.Entity;
using CraftParameterConfig.Domain.Interfaces;
using CraftParameterConfig.Infrastructure.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CraftParameterConfig.Infrastructure.Data
{
    public class TableNameRespository : RespositoryBase, ITableNameRespository
    {
        DbContext dbContext = GlobalObjects.DbContext;
        ObjectChange obc = GlobalObjects.ObjectChange;
        public TableNameRespository(IUnitOfWork unitOfWork)
            :base(unitOfWork)
        {}
        public void Add(TableNameConfig entity)
        {
            string sql = GetInsertSql(entity);
            using (DbTransaction dbTransaction= _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void AddRange(List<TableNameConfig> entitys)
        {
            //此功能被注释,是因为MDA_PARAMETER_NAME表暂时不需要批量添加数据的功能
            //List<string> sql = new List<string>();
            //foreach (var item in entitys)
            //{
            //    sql.Add(GetInsertSql(item));
            //}
            //using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            //{
            //    var com = dbContext.GetDbCommand(dbTransaction.Connection);
            //    dbContext.ExecuteSqlNoQuery(com, sql);
            //    _unitOfWork.SaveChange();
            //}
            throw new Exception("表MDA_PARAMETER_NAME不支持批量新增的功能,因为工艺数据的配置极度重要,需要专业的工程师进行配置!");
        }

        public void Delete(Guid id)
        {
            string sql = string.Format("update MDA_PARAMETER_NAME t set t.STATUS=1 where t.GUIDID='{0}'", id.ToString());
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void Delete(TableNameConfig entity)
        {
            Delete(entity.GuidID);
        }

        public void DeleteAll()
        {
            throw new Exception("您不能一次删除所有表,这是一个非常危险,同时也是非法的操作,此操作需要特别注意,请通过邮件,企业微信等方式通知您的直接领导,以获得授权!");
        }

        public TableNameConfig GetEntityByContition(string condition)
        {
            DataTable dtSource = GetTable(condition);
            if (dtSource.Rows.Count != 1)
            {
                throw new Exception("当前条件在TableNameConfig表中不能找到唯一一条记录,请重新设置条件再进行查询!");
            }
            return obc.ChangeTableToObject<TableNameConfig>(dtSource).FirstOrDefault();
        }

        public TableNameConfig GetEntityByGuid(Guid id)
        {
            string sql = string.Format("where t.GUIDID='{0}' and t.STATUS = 0", id.ToString());
            DataTable dtSource = GetTable(sql);
            if (dtSource.Rows.Count != 1)
            {
                throw new Exception("当前条件在TableNameConfig表中不能找到唯一一条记录,请重新设置条件再进行查询!");
            }
            return obc.ChangeTableToObject<TableNameConfig>(dtSource).FirstOrDefault();
        }

        public List<TableNameConfig> GetEntitysByContition(string condition)
        {
            DataTable dtSource = GetTable(condition);
            return obc.ChangeTableToObject<TableNameConfig>(dtSource).ToList();
        }

        public DataTable GetTable(string condition)
        {
            string sql = string.Format("select * from MDA_PARAMETER_NAME t {0} order by t.CREATE_TIME desc", condition);
            var con = dbContext.GetDbConnection();
            try
            {
                con.Open();
                return dbContext.GetTable(dbContext.GetDbCommand(con), sql);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public void Update(TableNameConfig entity)
        {
            throw new Exception("表MDA_PARAMETER_NAME不支持更新功能,因为任何对此表的修改,都有可能造成难以预料的异常,甚至可能造成系统崩溃!");
        }
        private string GetInsertSql(TableNameConfig table)
        {
            return string.Format("Insert into MDA_PARAMETER_NAME(NAME,GUIDID,CREATE_USER)values('{0}','{1}','{2}')", table.Name, table.GuidID.ToString(), table.CREATE_USER);
        }
    }
}
